<?php
	error_reporting(0);

	if(!empty($_REQUEST['send'])&&!empty($_REQUEST['password'])&&!empty($_REQUEST['username'])&&!empty($_REQUEST['qq'])){
		if(preg_match("/^[a-zA-Z0-9]+$/",$_REQUEST['username'])&&preg_match("/^[a-zA-Z0-9]+$/",$_REQUEST['password'])&&preg_match("/^[0-9]+$/",$_REQUEST['qq'])&&strlen($_REQUEST['username']) <= 32&&is_numeric(trim($_REQUEST['qq']))&&strlen($_REQUEST['qq']) <= 16){
			$conn = mysql_connect("localhost","root","password");
			$bool = true;
			$rs = mysql_list_tables("users"); 
			while($row = mysql_fetch_row($rs)){
				if($row[0] == $_REQUEST['username']){
					$bool = false;
				}
 			}  
			mysql_free_result($rs);
			if($bool&&$_REQUEST['send'] == "reg"){
				//成功！code 200
				mysql_select_db("users");
				mysql_query(
					"CREATE TABLE ".$_REQUEST['username']." 
					(
					username varchar(32),
					password varchar(32),
					qq varchar(16)
					)",$conn);
				mysql_query("INSERT INTO ".$_REQUEST['username']." (username,qq,password) VALUES ('".$_REQUEST['username']."','".$_REQUEST['qq']."','".md5($_REQUEST['password'])."')");
				mysql_close($conn);
				die(json_encode(Array("code"=>200)));
			}else if(!$bool && $_REQUEST['send'] == 'login'){
				mysql_select_db("users");
				$result = mysql_query("SELECT * FROM ".$_REQUEST['username']);
				while($row = mysql_fetch_array($result)){
					$password = $row['password'];
				}
				if($password == md5($_REQUEST['password'])){
					mysql_close($conn);
					setcookie("username",$_REQUEST['username']);
					setcookie("password",md5($_REQUEST['password']));
					die(json_encode(Array("code"=>200)));
				}
				mysql_close($conn);
				die(json_encode(Array("code"=>600)));
			}else if(!empty($_REQUEST['send'])){
				mysql_close($conn);
				die(json_encode(Array("code"=>500)));
			}
		}else if(!empty($_REQUEST['send'])){
			die(json_encode(Array("code"=>400)));
		}
	}else if(!empty($_REQUEST['send'])){
		die(json_encode(Array("code"=>300)));
	}
?>